package cn.com.cetccst.appstore.database.dao;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Query;

import java.util.List;

import cn.com.cetccst.appstore.database.model.AppModel;

@Dao
public interface AppModelDao extends BaseDao<AppModel> {

    @Query("DELETE FROM " + AppModel.TABLE_NAME)
    void clearAll();

    @Query("SELECT * FROM " + AppModel.TABLE_NAME + " WHERE app_name LIKE :searchKey and category_id LIKE :category ORDER BY create_time DESC")
    LiveData<List<AppModel>> loadAllApps(String category, String searchKey);

    @Query("SELECT count(*) from " + AppModel.TABLE_NAME + " WHERE need_upgrade = 1")
    LiveData<Integer> countNeedUpgradeApps();

    @Query("SELECT * FROM " + AppModel.TABLE_NAME + " WHERE need_upgrade = 1 ORDER BY create_time DESC")
    LiveData<List<AppModel>> loadAllNeedUpgradeApps();

    @Query("SELECT * FROM " + AppModel.TABLE_NAME + " WHERE id = :appId LIMIT 1")
    LiveData<AppModel> loadAppByAppId(String appId);

    @Query("SELECT * FROM " + AppModel.TABLE_NAME + " WHERE id = :appId LIMIT 1")
    AppModel getAppByAppId(String appId);

    @Query("SELECT * FROM " + AppModel.TABLE_NAME + " WHERE package_name = :packageName LIMIT 1")
    AppModel getAppByPackageName(String packageName);
    @Query("SELECT * FROM " + AppModel.TABLE_NAME + " WHERE package_name = :packageName LIMIT 1")
    LiveData<AppModel> loadAppByPackageName(String packageName);
}
